using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Blog.IRepository;
using Blog.IService;
using Blog.Repository;
using Blog.Service;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;

namespace Blog.Command.Extensions
{
    public static class IOC
    {
        public static IServiceCollection ExtensionsIOC(this IServiceCollection service)
        {   
            service.AddScoped<IBlogNewsRepository, BlogNewsRepository>();
            service.AddScoped<IBlogNewsService, BlogNewsService>();
            service.AddScoped<ITypeInfoRepository, TypeInfoRepository>();
            service.AddScoped<ITypeInfoService,TypeInfoService>();
            service.AddScoped<IWriterInfoRepository, WriterInfoRepository>();
            service.AddScoped<IWriterInfoService, WriterInfoService>();
            return service;
        }

        public static IServiceCollection DiscriminateJwt(this IServiceCollection services)
        {
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options => 
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("aAdfe11Fwfarasff4d5A2")),
                        ValidateIssuer = true,
                        ValidIssuer = "http://localhost:5183",
                        ValidateAudience = true,
                        ValidAudience = "http://localhost:5159",
                        ValidateLifetime = true,
                        ClockSkew = TimeSpan.FromMinutes(60)
                    };
                }); 
            return services;
        }
    }
}